// kloader.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "kloader.h"
#include <sstream>

#include "..\khelper\khelper.h"
#pragma comment(lib,"..\\Debug\\khelper.lib")

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	using namespace std;
	wstringstream ss;
	ss<<lpCmdLine;
	wstring ind;

	bool create=false;
	bool silent=false;
	wstring target;
	wstring dll;

	while(ss>>ind){
		if(ind==L"-target"){
			ss>>target;
		}else if(ind==L"-dll"){
			ss>>dll;
		}else if(ind==L"-create"){
			create=true;
		}else if(ind==L"-silent"){
			silent=true;
		}
	}
	if(target==L""){
		MessageBox(0,_T("kloader [-create] [-silent] -dll (dllname) -target (targetname)"),L"help",MB_OK);
		return 0;
	}
	kDebugMode(TRUE);
	
	ss.clear();
	ss.str(L"");

	if(create){
		wchar_t tmp[1024]={0};
		STARTUPINFO si={sizeof(si)};
		PROCESS_INFORMATION pi;
		ss<<L"create "<<target<<L" "<<dll<<kCreateProcessW(target.c_str(),tmp,0,0,0,0,0,0,&si,&pi,dll.c_str(),0);
	}else{
		ss<<target<<L" "<<dll<<kRemoteLoadLibraryWithFileW(target.c_str(),dll.c_str());
	}

	if(!silent)	MessageBox(0,ss.str().c_str(),0,0);
	return 0;
}

